﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;

namespace SaladBar.Service.Repositories.Utilities
{
    public class OrderBuilder<T>
    {
        private Func<IQueryable<T>, IQueryable<T>> _order;

        public OrderBuilder<T> Ascending(Expression<Func<T, object>> expression)
        {
            if (this._order == null)
                _order = (ts) => ts.OrderBy(expression);
            else
                this._order = (ts) => ((IOrderedQueryable<T>)_order(ts)).ThenBy(expression);

            return this;
        }

        public OrderBuilder<T> Descending(Expression<Func<T, object>> expression)
        {
            if (this._order == null)
                _order = (ts) => ts.OrderByDescending(expression);
            else
                this._order = (ts) => ((IOrderedQueryable<T>)_order(ts)).ThenByDescending(expression);

            return this;
        }

        public Func<IQueryable<T>, IQueryable<T>> Build()
        {
            return _order;
        }
    }
}
